﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using YArchitech.Revit.RevitExtension;

namespace YArchitech.HVAC
{
	public class OpearSumpSectionsAnnotation
	{
		public OpearSumpSectionsAnnotation(Document document, List<SumpParamSet> sumParaSets)
		{
			DataReadWrite.InitializeFilePath(this.xmlPath);
			this.doc = document;
			this.sumParamSets = sumParaSets;
			this.revitVer = RevitVersion.GetVersionNumber(this.doc);
			this.CopyFamilyTemplet();
		}

		private void CopyFamilyTemplet()
		{
			Transaction transaction = new Transaction(this.doc, "复制模板族");
			try
			{
				transaction.Start();
				this.textNodeType = this.GetTextNoteTypeByName(this.doc, "3.5mm 常规_仿宋");
				if (!this.textNodeType.GetParameter("引线/边界偏移量").IsReadOnly && this.textNodeType.GetParameter("引线/边界偏移量") != null)
				{
					this.textNodeType.GetParameter("引线/边界偏移量").Set(0);
				}
				if (!this.textNodeType.GetParameter("文字大小").IsReadOnly && this.textNodeType.GetParameter("文字大小") != null)
				{
					this.textNodeType.GetParameter("文字大小").Set(0.00984251968503937);
				}
				this.dimensionType = this.GetDimTypeByName(this.doc, "HY-3.5-长仿宋-0.8(下左)", "对角线 1.0mm");
				if (!this.dimensionType.GetParameter("尺寸界线延伸").IsReadOnly && this.dimensionType.GetParameter("尺寸界线延伸") != null)
				{
					this.dimensionType.GetParameter("尺寸界线延伸").Set(0.0032808398950131233);
				}
				if (!this.dimensionType.GetParameter("尺寸界线与图元的间隙").IsReadOnly && this.dimensionType.GetParameter("尺寸界线与图元的间隙") != null)
				{
					this.dimensionType.GetParameter("尺寸界线与图元的间隙").Set(0.0016404199475065617);
				}
				if (!this.dimensionType.GetParameter("文字大小").IsReadOnly && this.dimensionType.GetParameter("文字大小") != null)
				{
					this.dimensionType.GetParameter("文字大小").Set(0.00984251968503937);
				}
				this.fsDiameter = this.GetFamilySymbol(this.doc, "MarkTag", "HY_水管标注.rfa", "水管管径");
				this.spotDimensionUpType = this.GetFamilySymbolyByName("MarkTag", "符号_普通标高(右上).rfa");
				this.spotDimensionDownType = this.GetFamilySymbolyByName("MarkTag", "符号_普通标高(右下).rfa");
				transaction.Commit();
				if (this.spotDimensionUpType == null || this.spotDimensionDownType == null || this.spotDimensionDownType == null || this.dimensionType == null)
				{
					YJKMessageBox.Show("项目和模板中均不包含指定类型");
				}
                MarkOperation markOperation = new MarkOperation();
				CommonFont commonFont = new CommonFont
				{
					Font = "仿宋",
					FontHeight = 3.0,
					Align = "始端对齐",
					FontGWRotia = 0.7,
					LeadDistance = 0.0,
					LeadRotia = 1.0,
					Presice = "0.000",
					TubeDistance = 0.0
				};
				markOperation.EditHYFamilyFont(this.fsDiameter.Family, commonFont);
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
		}

		public void Execute()
		{
			UIDocument uiDoc = new UIDocument(this.doc);
			ValveOperation valveOperation = new ValveOperation(uiDoc);
			try
			{
				Element sump = this.GetSump(uiDoc);
				this.DeleteTags(sump);
				this.line = this.CreatSopt(sump, ref this.length);
				this.CreatTagging(this.doc, this.tagReferLines);
				if (!sump.Document.IsLinked)
				{
					valveOperation.ValveConnection(sump, this.line, this.textNodeType);
				}
				else
				{
					YJKMessageBox.Show("标注完成，未搜到水泵或链接文件中无法布置组合阀件！");
				}
				this.RecordLastTimeTags(sump);
				this.doc.Regenerate();
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
		}

		private Element GetSump(UIDocument uiDoc)
		{
			Element element;
			do
			{
				SelectedElement selectedElement = uiDoc.PickElementsByPoint("请拾取集水坑坑底...", 0.05);
				element = selectedElement.Element;
				this.refGlobalPoint = selectedElement.SelectedPoint;
			}
			while (!this.ElemFilter(element));
			return element;
		}

		private Line CreatSopt(Element sumpElem, ref double length)
		{
			string text = sumpElem.GetParameter(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM).AsValueString();
			XYZ xyz = (sumpElem.Location as LocationPoint).Point;
			if (sumpElem.Document.IsLinked)
			{
				xyz = this.doc.GetLinkedDocument(sumpElem.Id).GetPointInLinkDocument(xyz);
			}
			sumpElem.GetParameter("深度").AsDouble();
			this.sumpThickness = sumpElem.GetParameter("壁厚").AsDouble();
			double num = 0.0;
			double num2 = 0.0;
			double num3 = 0.0;
			XYZ xyz2 = new XYZ();
			if (text.Contains("方形集水坑"))
			{
				num = sumpElem.GetParameter("长度").AsDouble();
				num2 = sumpElem.GetParameter("宽度").AsDouble();
				xyz2 = xyz + (sumpElem as FamilyInstance).HandOrientation * (num / 2.0 + this.sumpThickness - 0.0225) - (sumpElem as FamilyInstance).FacingOrientation * (num2 / 2.0 + this.sumpThickness - 0.0225);
			}
			else if (text.Contains("圆形集水坑"))
			{
				num3 = sumpElem.GetParameter("直径").AsDouble();
				xyz2 = xyz;
			}
			else if (text.Contains("排水沟"))
			{
				num = sumpElem.GetParameter("长度").AsDouble();
				num2 = sumpElem.GetParameter("宽度").AsDouble();
				xyz2 = xyz;
			}
			Line line = Line.CreateUnbound(this.refGlobalPoint, this.doc.ActiveView.RightDirection);
			XYZ xyzpoint = line.Project(xyz2).XYZPoint;
			XYZ xyz3 = new XYZ();
			XYZ xyz4 = new XYZ();
			if (text.Contains("圆形"))
			{
				xyz3 = xyzpoint - this.doc.ActiveView.RightDirection * (num3 / 2.0 + this.sumpThickness);
				xyz4 = xyzpoint + this.doc.ActiveView.RightDirection * (num3 / 2.0 + this.sumpThickness);
			}
			else if ((sumpElem as FamilyInstance).HandOrientation.IsAlmostEqualTo(this.doc.ActiveView.RightDirection))
			{
				xyz3 = xyzpoint - this.doc.ActiveView.RightDirection * (num / 2.0 + this.sumpThickness);
				xyz4 = xyzpoint + this.doc.ActiveView.RightDirection * (num / 2.0 + this.sumpThickness);
			}
			else
			{
				xyz3 = xyzpoint - this.doc.ActiveView.RightDirection * (num2 / 2.0 + this.sumpThickness);
				xyz4 = xyzpoint + this.doc.ActiveView.RightDirection * (num2 / 2.0 + this.sumpThickness);
			}
			length = xyz3.DistanceTo(xyz4);
			this.tagReferLines.Add(this.CreaBottomSpotDime(this.doc, xyz3, xyz4, "(坑底标高)"));
			foreach (SumpParamSet sumpParamSet in this.sumParamSets)
			{
				DetailLine detailLine;
				if (sumpParamSet.ParamName == "开泵液位")
				{
					detailLine = this.CreaSpotDimeAndLiqLev(this.doc, xyz3, xyz4, sumpParamSet.RelativeHeight, "(" + sumpParamSet.ParamName + ")", false);
					this.tagReferLines.Add(detailLine);
				}
				else
				{
					detailLine = this.CreaSpotDimeAndLiqLev(this.doc, xyz3, xyz4, sumpParamSet.RelativeHeight, "(" + sumpParamSet.ParamName + ")", true);
					this.tagReferLines.Add(detailLine);
				}
				this.newgrpElements.Insert(detailLine);
			}
			return line;
		}

		private DetailLine CreaBottomSpotDime(Document document, XYZ startPoint, XYZ endPoint, string liquidLevName)
		{
			double num = (double)document.ActiveView.GetParameter(BuiltInParameter.VIEW_SCALE).AsInteger() / 100.0;
			XYZ xyz = document.ActiveView.UpDirection.Normalize();
			XYZ xyz2 = document.ActiveView.RightDirection.Normalize();
			XYZ xyz3 = endPoint + num * 0.32808398950131235 * xyz2;
			XYZ xyz4 = endPoint + num * (6.2335958005249346 + this.AddLengthTextNoteInDifferentRevitVer()) * xyz2;
			DetailLine result;
			using (SubTransaction subTransaction = new SubTransaction(document))
			{
				subTransaction.Start();
				DetailLine detailLine = YJKRevitTools.DrawDetailLine(document, xyz3, xyz4);
				this.newgrpElements.Insert(detailLine);
				AnnotationSymbol annotationSymbol = RevitVersionFuncs.NewAnnotationSymbol(this.doc, xyz4 - num * 0.59055118110236215 * xyz2, this.spotDimensionDownType, this.doc.ActiveView);
				if (this.IsNotRevit2017() && this.doc.ActiveView.RightDirection.X < this.doc.ActiveView.ViewDirection.Y)
				{
					Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(xyz4 - num * 0.59055118110236215 * xyz2, this.doc.ActiveView.RightDirection, this.doc.ActiveView.ViewDirection);
					List<ElementId> elementsToMirror = RevitVersionFuncs.MirrorElements(document, new List<ElementId>
					{
						annotationSymbol.Id
					}, plane, (BuiltInCategory)(-2000150), false);
					Plane plane2 = RevitVersionFuncs.CreatePlanByOriginAndBasis(xyz4 - num * 0.59055118110236215 * xyz2, this.doc.ActiveView.ViewDirection, this.doc.ActiveView.UpDirection);
					if (this.revitVer == "2016")
					{
						elementsToMirror = new List<ElementId>
						{
							annotationSymbol.Id
						};
					}
					List<ElementId> source = RevitVersionFuncs.MirrorElements(document, elementsToMirror, plane2, BuiltInCategory.OST_GenericAnnotation, false);
					if (this.revitVer == "2015" || this.revitVer == "2014")
					{
						annotationSymbol = (this.doc.GetElement(source.ElementAt(0)) as AnnotationSymbol);
					}
				}
				double value = xyz4.Z * 304.8 / 1000.0;
				annotationSymbol.GetParameter("标高1").Set(Math.Round(value, 3).ToString("0.000"));
				this.newgrpElements.Insert(annotationSymbol);
				this.newgrpElements.Insert(UnifiedModified.HYCreatTextNote(document, liquidLevName, xyz3 + xyz * (this.MoveTextNoteInDifferentRevitVer() + 1.1318897637795275 * num), this.textNodeType));
				subTransaction.Commit();
				this.doc.Regenerate();
				result = YJKRevitTools.DrawDetailLine(document, startPoint, startPoint + 0.0032808398950131233 * xyz2);
			}
			return result;
		}

		private DetailLine CreaSpotDimeAndLiqLev(Document document, XYZ startPoint, XYZ endPoint, double relativeHigh, string liquidLevName, bool upOrNot)
		{
			double num = (double)document.ActiveView.GetParameter(BuiltInParameter.VIEW_SCALE).AsInteger() / 100.0;
			XYZ xyz = (startPoint + endPoint) / 2.0;
			double num2 = endPoint.DistanceTo(startPoint);
			XYZ xyz2 = document.ActiveView.UpDirection.Normalize();
			XYZ xyz3 = document.ActiveView.RightDirection.Normalize();
			XYZ xyz4 = endPoint + num * 0.32808398950131235 * xyz3;
			XYZ xyz5 = endPoint + num * (6.2335958005249346 + this.AddLengthTextNoteInDifferentRevitVer()) * xyz3;
			using (SubTransaction subTransaction = new SubTransaction(document))
			{
				subTransaction.Start();
				if (!liquidLevName.Contains("地面标高"))
				{
					DetailLine detailLine = YJKRevitTools.DrawDetailLine(document, startPoint + xyz2 * relativeHigh, endPoint + xyz2 * relativeHigh);
					this.newgrpElements.Insert(detailLine);
					XYZ xyz6 = xyz - 0.1 * num2 * xyz3;
					XYZ xyz7 = xyz + 0.1 * num2 * xyz3;
					DetailLine detailLine2 = YJKRevitTools.DrawDetailLine(document, xyz6 + xyz2 * (relativeHigh - 0.05), xyz7 + xyz2 * (relativeHigh - 0.05));
					this.newgrpElements.Insert(detailLine2);
					XYZ xyz8 = xyz - 0.05 * num2 * xyz3;
					XYZ xyz9 = xyz + 0.05 * num2 * xyz3;
					DetailLine detailLine3 = YJKRevitTools.DrawDetailLine(document, xyz8 + xyz2 * (relativeHigh - 0.1), xyz9 + xyz2 * (relativeHigh - 0.1));
					this.newgrpElements.Insert(detailLine3);
				}
				DetailLine detailLine4 = YJKRevitTools.DrawDetailLine(document, xyz4 + xyz2 * relativeHigh, xyz5 + xyz2 * relativeHigh);
				this.newgrpElements.Insert(detailLine4);
				new Reference(detailLine4);
				AnnotationSymbol annotationSymbol;
				if (upOrNot)
				{
					annotationSymbol = RevitVersionFuncs.NewAnnotationSymbol(this.doc, xyz5 + xyz2 * relativeHigh - num * 0.59055118110236215 * xyz3, this.spotDimensionUpType, this.doc.ActiveView);
				}
				else
				{
					annotationSymbol = RevitVersionFuncs.NewAnnotationSymbol(this.doc, xyz5 + xyz2 * relativeHigh - num * 0.59055118110236215 * xyz3, this.spotDimensionDownType, this.doc.ActiveView);
				}
				if (this.IsNotRevit2017() && this.doc.ActiveView.RightDirection.X < this.doc.ActiveView.ViewDirection.Y)
				{
					Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(xyz5 + xyz2 * relativeHigh - num * 0.59055118110236215 * xyz3, this.doc.ActiveView.RightDirection, this.doc.ActiveView.ViewDirection);
					List<ElementId> elementsToMirror = RevitVersionFuncs.MirrorElements(document, new List<ElementId>
					{
						annotationSymbol.Id
					}, plane, BuiltInCategory.OST_GenericAnnotation, false);
					Plane plane2 = RevitVersionFuncs.CreatePlanByOriginAndBasis(xyz5 + xyz2 * relativeHigh - num * 0.59055118110236215 * xyz3, this.doc.ActiveView.ViewDirection, this.doc.ActiveView.UpDirection);
					if (this.revitVer == "2016")
					{
						elementsToMirror = new List<ElementId>
						{
							annotationSymbol.Id
						};
					}
					List<ElementId> source = RevitVersionFuncs.MirrorElements(document, elementsToMirror, plane2, BuiltInCategory.OST_GenericAnnotation, false);
					if (this.revitVer == "2015" || this.revitVer == "2014")
					{
						annotationSymbol = (this.doc.GetElement(source.ElementAt(0)) as AnnotationSymbol);
					}
				}
				this.newgrpElements.Insert(annotationSymbol);
				double value = (xyz5.Z + relativeHigh) * 304.8 / 1000.0;
				annotationSymbol.GetParameter("标高1").Set(Math.Round(value, 3).ToString("0.000"));
				this.newgrpElements.Insert(UnifiedModified.HYCreatTextNote(document, liquidLevName, xyz4 + xyz2 * (relativeHigh + this.MoveTextNoteInDifferentRevitVer() + 1.1318897637795275 * num), this.textNodeType));
				subTransaction.Commit();
				this.doc.Regenerate();
			}
			return YJKRevitTools.DrawDetailLine(document, startPoint + xyz2 * relativeHigh, startPoint + xyz3 * 0.0032808398950131233 + xyz2 * relativeHigh);
		}

		private void CreatTagging(Document document, List<DetailLine> taggReferLines)
		{
			LocationCurve locationCurve = taggReferLines.ElementAt(0).Location as LocationCurve;
			LocationCurve locationCurve2 = taggReferLines.ElementAt(taggReferLines.Count - 1).Location as LocationCurve;
			(locationCurve.Curve.GetEndPoint(1) - locationCurve.Curve.GetEndPoint(0)).Normalize();
			Line line = Line.CreateBound(locationCurve.Curve.GetEndPoint(0) - document.ActiveView.RightDirection * 1.1482939632545932, locationCurve2.Curve.GetEndPoint(0) - document.ActiveView.RightDirection * 1.1482939632545932);
			Line line2 = Line.CreateBound(locationCurve.Curve.GetEndPoint(0) - document.ActiveView.RightDirection * 1.9685039370078738, locationCurve2.Curve.GetEndPoint(0) - document.ActiveView.RightDirection * 1.9685039370078738);
			ReferenceArray referenceArray = new ReferenceArray();
			foreach (DetailLine detailLine in taggReferLines)
			{
				Reference reference = new Reference(detailLine);
				referenceArray.Append(reference);
			}
			ReferenceArray referenceArray2 = new ReferenceArray();
			referenceArray2.Append(referenceArray.get_Item(0));
			referenceArray2.Append(referenceArray.get_Item(referenceArray.Size - 1));
			using (SubTransaction subTransaction = new SubTransaction(document))
			{
				document.Regenerate();
				subTransaction.Start();
				Dimension dimension = document.Create.NewDimension(document.ActiveView, line, referenceArray, this.dimensionType);
				RevitDimensionExtension.ResetDimTextPos(dimension);
				Dimension dimension2 = document.Create.NewDimension(document.ActiveView, line2, referenceArray2, this.dimensionType);
				RevitDimensionExtension.ResetDimTextPos(dimension2);
				this.newgrpElements.Insert(dimension);
				this.newgrpElements.Insert(dimension2);
				subTransaction.Commit();
			}
		}

		private bool ElemFilter(Element elem)
		{
			string text = elem.GetParameter(BuiltInParameter.ELEM_FAMILY_AND_TYPE_PARAM).AsValueString();
			return elem.Category.Id.IntegerValue == -2001350 && (text.Contains("集水坑") || text.Contains("排水沟"));
		}

		public void CerateTagElemsGroup()
		{
			if (this.newgrpElements.Size < 1)
			{
				return;
			}
			RevitVersionFuncs.NewGroup(this.doc, this.newgrpElements);
		}

		public TextNoteType GetTextNoteTypeByName(Document document, string name)
		{
			TextNoteType textNoteType = null;
			foreach (Element element in new FilteredElementCollector(document).OfClass(typeof(TextNoteType)).ToElements())
			{
				TextNoteType textNoteType2 = (TextNoteType)element;
				if (textNoteType2.Name.Equals(name))
				{
					textNoteType = textNoteType2;
					break;
				}
			}
			if (textNoteType == null)
			{
				textNoteType = (this.CopyTypeFromTemplate(document, typeof(TextNoteType), name, "") as TextNoteType);
			}
			return textNoteType;
		}

		public SpotDimensionType GetSoptDimensTypeByName(Document document, string name)
		{
			SpotDimensionType spotDimensionType = null;
			foreach (Element element in new FilteredElementCollector(document).OfClass(typeof(SpotDimensionType)).ToElements())
			{
				SpotDimensionType spotDimensionType2 = (SpotDimensionType)element;
				if (spotDimensionType2.Name.Equals(name))
				{
					spotDimensionType = spotDimensionType2;
					break;
				}
			}
			if (spotDimensionType == null)
			{
				spotDimensionType = (this.CopyTypeFromTemplate(document, typeof(SpotDimensionType), name, "") as SpotDimensionType);
			}
			return spotDimensionType;
		}

		private DimensionType GetDimTypeByName(Document document, string strLabel, string strMark)
		{
			DimensionType dimensionType = null;
			foreach (Element element in new FilteredElementCollector(document).OfClass(typeof(DimensionType)).ToElements())
			{
				if (element.Name == strLabel)
				{
					dimensionType = (element as DimensionType);
					break;
				}
			}
			if (dimensionType == null)
			{
				dimensionType = (this.CopyTypeFromTemplate(document, typeof(DimensionType), strLabel, strMark) as DimensionType);
			}
			return dimensionType;
		}

		public Element CopyTypeFromTemplate(Document doc, Type targetType, string strLabel, string strMark)
		{
			Element result = null;
			string versionNumber = RevitVersion.GetVersionNumber(doc);
			string text = Path.Combine(Product.FamilyLocation, versionNumber, "Template", "HYBIMSpace给排水样板.rte");
			Document document = null;
			if (!File.Exists(text))
			{
				return null;
			}
			document = doc.Application.OpenDocumentFile(text);
			Element element = null;
			foreach (Element element2 in new FilteredElementCollector(document).OfClass(targetType).ToElements())
			{
				if (element2.Name == strLabel)
				{
					element = element2;
					break;
				}
			}
			Element element3 = null;
			if (strMark.Trim() != "")
			{
                foreach (Element element4 in new FilteredElementCollector(document).OfClass(typeof(Autodesk.Revit.DB.ElementType)).ToElements())
				{
					if (element4.Name == strMark)
					{
						element3 = element4;
						break;
					}
				}
			}
			if (element == null && element3 == null)
			{
				return null;
			}
			if (element3 != null)
			{
				List<ElementId> list = new List<ElementId>();
				list.Add(element.Id);
				List<ElementId> list2 = new List<ElementId>();
				list2.Add(element3.Id);
				SubTransaction subTransaction = new SubTransaction(doc);
				subTransaction.Start();
				try
				{
					ICollection<ElementId> collection = ElementTransformUtils.CopyElements(document, list, doc, Transform.Identity, new CopyPasteOptions());
					ElementTransformUtils.CopyElements(document, list2, doc, Transform.Identity, new CopyPasteOptions());
					subTransaction.Commit();
					if (collection.Count > 0)
					{
						result = doc.GetElement(collection.ElementAt(0));
					}
					goto IL_209;
				}
				catch
				{
					subTransaction.RollBack();
					goto IL_209;
				}
			}
			List<ElementId> list3 = new List<ElementId>();
			list3.Add(element.Id);
			SubTransaction subTransaction2 = new SubTransaction(doc);
			subTransaction2.Start();
			try
			{
				ICollection<ElementId> collection2 = ElementTransformUtils.CopyElements(document, list3, doc, Transform.Identity, new CopyPasteOptions());
				subTransaction2.Commit();
				if (collection2.Count > 0)
				{
					result = doc.GetElement(collection2.ElementAt(0));
				}
			}
			catch
			{
				subTransaction2.RollBack();
			}
			IL_209:
			document.Close();
			return result;
		}

		private FamilySymbol GetFamilySymbol(Document Doc, string typeName, string familyName, string familySymbolName)
		{
			FamilySymbol result = null;
			string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(Doc), typeName, familyName);
			Family family = null;
			SubTransaction subTransaction = null;
			try
			{
				foreach (Element element in YJKElementFilter.GetElementByCategory(Doc, typeof(FamilySymbol), BuiltInCategory.OST_PipeTags))
				{
					if ((element as FamilySymbol).Family.Name == familyName.Substring(0, familyName.IndexOf(".")) && element.Name == familySymbolName)
					{
						FamilySymbol result2;
						result = (result2 = (element as FamilySymbol));
						return result2;
					}
				}
				subTransaction = new SubTransaction(Doc);
				subTransaction.Start();
				Doc.LoadFamily(text, out family);
				subTransaction.Commit();
			}
			catch (Exception)
			{
			}
			finally
			{
				if (subTransaction != null && (int)subTransaction.GetStatus() != 3)
				{
					subTransaction.RollBack();
				}
			}
			if (family != null)
			{
				foreach (FamilySymbol familySymbol in family.GetSymbols())
				{
					if (familySymbol.Name == familySymbolName)
					{
						result = familySymbol;
						break;
					}
				}
			}
			return result;
		}

		public AnnotationSymbolType GetFamilySymbolyByName(string typeName, string familyName)
		{
			AnnotationSymbolType result = null;
			string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(this.doc), typeName, familyName);
			Family family = null;
			SubTransaction subTransaction = null;
			try
			{
				foreach (AnnotationSymbolType annotationSymbolType in (from ele in RevitFilter.GetElementsOfType(this.doc, typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_GenericAnnotation)
				select (AnnotationSymbolType)ele).ToList<AnnotationSymbolType>())
				{
					if (annotationSymbolType.Family.Name == familyName.Substring(0, familyName.IndexOf(".")))
					{
						AnnotationSymbolType result2;
						result = (result2 = annotationSymbolType);
						return result2;
					}
				}
				subTransaction = new SubTransaction(this.doc);
				subTransaction.Start();
				this.doc.LoadFamily(text, out family);
				subTransaction.Commit();
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
			finally
			{
                if (subTransaction != null && (int)(int)subTransaction.GetStatus() != 3)
				{
					subTransaction.RollBack();
				}
			}
			if (family != null)
			{
				using (List<FamilySymbol>.Enumerator enumerator2 = family.GetSymbols().GetEnumerator())
				{
					if (enumerator2.MoveNext())
					{
						result = (enumerator2.Current as AnnotationSymbolType);
					}
				}
			}
			return result;
		}

		public void RecordLastTimeTags(Element elem)
		{
			this.IsLinkedDocRcordTagElems(elem);
		}

		private void IsLinkedDocRcordTagElems(Element elem)
		{
			Guid guid = new Guid("0E61FE42-3A3F-4DD0-8EE3-D8268D3D39B9");
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("$" + elem.Id.IntegerValue.ToString());
			foreach (object obj in this.newgrpElements)
			{
				Element element = (Element)obj;
				stringBuilder.Append("@" + element.Id.IntegerValue.ToString());
			}
			if (Schema.Lookup(guid) == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetReadAccessLevel((AccessLevel)1);
				schemaBuilder.SetWriteAccessLevel((AccessLevel)1);
				schemaBuilder.SetSchemaName("SumpRecordTagElems");
				schemaBuilder.SetDocumentation("Data store for Tags info in a water pit");
				schemaBuilder.AddSimpleField("SumpRecordTagElems", typeof(string));
				Schema schema = schemaBuilder.Finish();
				Entity entity = new Entity(schema);
				schema.GetField("SumpRecordTagElems");
				entity.Set<string>("SumpRecordTagElems", stringBuilder.ToString());
				this.doc.ActiveView.SetEntity(entity);
				return;
			}
			Entity entity2 = this.doc.ActiveView.GetEntity(Schema.Lookup(guid));
			if (entity2.Schema == null)
			{
				Schema schema2 = Schema.ListSchemas().FirstOrDefault((Schema p) => p.SchemaName == "SumpRecordTagElems");
				if (schema2 != null)
				{
					Entity entity3 = new Entity(schema2);
					schema2.GetField("SumpRecordTagElems");
					entity3.Set<string>("SumpRecordTagElems", stringBuilder.ToString());
					this.doc.ActiveView.SetEntity(entity3);
					return;
				}
			}
			else
			{
				string text = entity2.Get<string>(Schema.Lookup(guid).GetField("SumpRecordTagElems"));
				Schema schema3 = Schema.ListSchemas().FirstOrDefault((Schema p) => p.SchemaName == "SumpRecordTagElems");
				if (schema3 != null)
				{
					if (string.IsNullOrEmpty(text))
					{
						schema3.GetField("SumpRecordTagElems");
						entity2.Set<string>("SumpRecordTagElems", stringBuilder.ToString());
					}
					else
					{
						schema3.GetField("SumpRecordTagElems");
						entity2.Set<string>("SumpRecordTagElems", text + stringBuilder.ToString());
					}
					this.doc.ActiveView.SetEntity(entity2);
				}
			}
		}

		private void RecordTagsIsNotLinkDoc(Element elem)
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (object obj in this.newgrpElements)
			{
				Element element = (Element)obj;
				stringBuilder.Append("@" + element.Id.IntegerValue.ToString());
			}
			Guid guid = new Guid("0E61FE42-3A3F-4DD0-8EE3-D8268D3D39B9");
			Schema schema;
			if (Schema.Lookup(guid) == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetReadAccessLevel((AccessLevel)1);
				schemaBuilder.SetWriteAccessLevel((AccessLevel)1);
				schemaBuilder.SetSchemaName("SumpRecordTagElems");
				schemaBuilder.SetDocumentation("Data store for Tags info in a water pit");
				schemaBuilder.AddSimpleField("SumpRecordTagElems", typeof(string));
				schema = schemaBuilder.Finish();
			}
			else
			{
				schema = Schema.Lookup(guid);
			}
			Entity entity = new Entity(schema);
			schema.GetField("SumpRecordTagElems");
			entity.Set<string>("SumpRecordTagElems", stringBuilder.ToString());
			elem.SetEntity(entity);
		}

		public void DeleteTags(Element element)
		{
			Guid guid = new Guid("0E61FE42-3A3F-4DD0-8EE3-D8268D3D39B9");
			if (Schema.Lookup(guid) == null)
			{
				return;
			}
			Entity entity = this.doc.ActiveView.GetEntity(Schema.Lookup(guid));
			if (entity.Schema == null)
			{
				return;
			}
			string text = entity.Get<string>(Schema.Lookup(guid).GetField("SumpRecordTagElems"));
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			char[] separator = new char[]
			{
				'$'
			};
			string[] array = text.ToString().Split(separator, StringSplitOptions.RemoveEmptyEntries);
			List<string> list = new List<string>();
			foreach (string text2 in array)
			{
				if (text2.StartsWith(element.Id.IntegerValue.ToString()))
				{
					this.DeleteTags(text2, element.Id.IntegerValue.ToString());
				}
				else
				{
					list.Add(text2);
				}
			}
			StringBuilder stringBuilder = new StringBuilder();
			foreach (string str in list)
			{
				stringBuilder.Append("$" + str);
			}
			Schema.Lookup(guid).GetField("SumpRecordTagElems");
			entity.Set<string>("SumpRecordTagElems", stringBuilder.ToString());
		}

		private void DeleteTags(string tagIds, string elemIdIntegerValue)
		{
			char[] separator = new char[]
			{
				'@'
			};
			foreach (string text in tagIds.ToString().Split(separator, StringSplitOptions.RemoveEmptyEntries))
			{
				int num;
				if (!text.Equals(elemIdIntegerValue) && int.TryParse(text, out num))
				{
					ElementId elementId = new ElementId(num);
					Element element = this.doc.GetElement(elementId);
					try
					{
						if (element != null)
						{
							this.doc.DeleteElement(element);
						}
					}
					catch (Exception)
					{
					}
				}
			}
		}

		private bool IsNotRevit2017()
		{
			return new List<string>
			{
				"2016",
				"2015",
				"2014"
			}.Contains(this.revitVer);
		}

		private double MoveTextNoteInDifferentRevitVer()
		{
			double result = 0.0;
			string a = this.revitVer;
			if (!(a == "2017") && !(a == "2018"))
			{
				if (!(a == "2016"))
				{
					if (a == "2015")
					{
						result = -0.29527559055118108;
					}
				}
				else
				{
					result = 0.0;
				}
			}
			else
			{
				result = 0.32808398950131235;
			}
			return result;
		}

		private double AddLengthTextNoteInDifferentRevitVer()
		{
			double result = 0.0;
			string a = this.revitVer;
			if (!(a == "2017") && !(a == "2018"))
			{
				if (!(a == "2016"))
				{
					if (a == "2015")
					{
						result = 0.0;
					}
				}
				else
				{
					result = 0.0;
				}
			}
			else
			{
				result = 0.65616797900262469;
			}
			return result;
		}

		private Document doc;

		private List<SumpParamSet> sumParamSets = new List<SumpParamSet>();

		private List<DetailLine> tagReferLines = new List<DetailLine>();

		private AnnotationSymbolType spotDimensionUpType;

		private AnnotationSymbolType spotDimensionDownType;

		private FamilySymbol fsDiameter;

		private TextNoteType textNodeType;

		private DimensionType dimensionType;

		private ElementSet newgrpElements = new ElementSet();

		private Line line;

		private double length;

		private double sumpThickness;

		private string revitVer = "";

		private string xmlPath = Path.Combine(Product.DataLocation, "HYAirSystemSetting.xml");

		private XYZ refGlobalPoint = new XYZ();
	}
}
